package cn.edu.tjufe.llp.tin.model;

import java.io.Serializable;
import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.Version;

import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity implements Serializable {


	/**
	 * 
	 */
	private static final long serialVersionUID = 7618889889474835263L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@Column(nullable = false)
	private Long deleted;

	@Version
	@Column(nullable = false)
	protected Long version;

	/**
     * 创建时间
     * 如果在数据库中，添加默认值"CURRENT_TIMESTAMP"，由数据库管理，则不需要使用注解。
     */
	@CreatedDate
	@Column(name = "created_at", nullable = false, updatable = false)   //不加这个的话，修改时会被改掉
	protected LocalDateTime createdAt;
	
    /**
     * 创建人
     */
    @CreatedBy
    @Column(name = "created_by", updatable = false)
    private Long createdBy;	

    /**
     * 修改时间
     * 如果在数据库中，添加默认值"CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"，由数据库管理，则不需要使用注解。
     */
    @LastModifiedDate
    @Column(name = "modified_at", nullable = false)
    private LocalDateTime modifiedAt;
    
    /**
     * 修改人
     */
    @LastModifiedBy
    @Column(name = "modified_by")
    private Long modifiedBy;

	@PrePersist
	protected void prePersist(){
		if (deleted == null) {
			deleted = 0L;
		}
	}    
}
